﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerThongKeTaiSanDao : IThongKeTaiSanDao
    {
        public SqlServerThongKeTaiSanDao()
        {
            SQLUtility.TableName = "ThongKeTaiSan";
        }

        //Trả về đối tượng báo ăn
        private ThongKeTaiSan MakeThongKeTaiSan(DataRow row)
        {
            long _id = long.Parse(row["Id"].ToString());
            Guid _schoolYearID = Guid.Parse(row["SchoolYearID"].ToString());
            Guid _classID = Guid.Parse(row["ClassID"].ToString());
            long _taiSanID = long.Parse(row["TaiSanID"].ToString());
            string _donVi = row["DonVi"].ToString();
            int _soluongDaunam = int.Parse(row["SoluongDaunam"].ToString());
            decimal _dongiaDaunam = decimal.Parse(row["DongiaDaunam"].ToString());
            double _tylehaomonDaunam = double.Parse(row["TylehaomonDaunam"].ToString());
            int _soluongCuoinam = int.Parse(row["SoluongCuoinam"].ToString());
            decimal _dongiaCuoinam = decimal.Parse(row["DongiaCuoinam"].ToString());
            double _tylehaomonCuoinam = double.Parse(row["TylehaomonCuoinam"].ToString());
            string _ghichu = row["Ghichu"].ToString();


            return new ThongKeTaiSan(_id, _schoolYearID, _classID, _taiSanID, _donVi, _soluongDaunam, _dongiaDaunam, _tylehaomonDaunam, _soluongCuoinam, _dongiaCuoinam, _tylehaomonCuoinam, _ghichu);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<ThongKeTaiSan> MakeThongKeTaiSans(DataTable dt)
        {
            IList<ThongKeTaiSan> list = new List<ThongKeTaiSan>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeThongKeTaiSan(row));
            }
            return list;
        }
        private DbParameter[] Take(ThongKeTaiSan ThongKeTaiSan)
        {
            DbParameter[] parameters = new DbParameter[12];
            parameters[0] = new SqlParameter("@Id", ThongKeTaiSan.Id);
            parameters[1] = new SqlParameter("@SchoolYearID", ThongKeTaiSan.SchoolYearID);
            parameters[2] = new SqlParameter("@ClassID", ThongKeTaiSan.ClassID);
            parameters[3] = new SqlParameter("@TaiSanID", ThongKeTaiSan.TaiSanID);
            parameters[4] = new SqlParameter("@DonVi", ThongKeTaiSan.DonVi);
            parameters[5] = new SqlParameter("@SoluongDaunam", ThongKeTaiSan.SoluongDaunam);
            parameters[6] = new SqlParameter("@DongiaDaunam", ThongKeTaiSan.DongiaDaunam);
            parameters[7] = new SqlParameter("@TylehaomonDaunam", ThongKeTaiSan.TylehaomonDaunam);
            parameters[8] = new SqlParameter("@SoluongCuoinam", ThongKeTaiSan.SoluongCuoinam);
            parameters[9] = new SqlParameter("@DongiaCuoinam", ThongKeTaiSan.DongiaCuoinam);
            parameters[10] = new SqlParameter("@TylehaomonCuoinam", ThongKeTaiSan.TylehaomonCuoinam);
            parameters[11] = new SqlParameter("@Ghichu", ThongKeTaiSan.Ghichu);
            return parameters;
        }

        public IList<Business_Objects.ThongKeTaiSan> GetThongKeTaiSan()
        {
            SQLUtility.TableName = "ThongKeTaiSan";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeThongKeTaiSans(dt);
        }

        public Business_Objects.ThongKeTaiSan GetThongKeTaiSanByID(string ThongKeTaiSanID)
        {
            SQLUtility.TableName = "ThongKeTaiSan";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", ThongKeTaiSanID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeThongKeTaiSan(row);
            }
        }

        public void InsertThongKeTaiSan(Business_Objects.ThongKeTaiSan ThongKeTaiSan)
        {
            SQLUtility.TableName = "ThongKeTaiSan";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(ThongKeTaiSan), true);
        }

        public int UpdateThongKeTaiSan(Business_Objects.ThongKeTaiSan ThongKeTaiSan)
        {
            SQLUtility.TableName = "ThongKeTaiSan";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(ThongKeTaiSan)); }
            catch { return -1; }
        }

        public int DeleteThongKeTaiSan(Business_Objects.ThongKeTaiSan ThongKeTaiSan)
        {
            SQLUtility.TableName = "ThongKeTaiSan";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(ThongKeTaiSan)); }
            catch { return -1; }
        }


        public List<ThongKeTaiSan> GetThongKeTaiSanBySchoolId(int schoolId)
        {
 
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectThongKeTaiSansByAndSchoolID", parameters);
            return MakeThongKeTaiSans(dt).ToList(); 
  
        }
    }
}
